/*
    游戏通用特效
 */
class EffectUtil {
    private static _instance:EffectUtil;

    public static instance(){
        if(!this._instance)
        {
            this._instance = new EffectUtil();
        }
        return this._instance;
    }

    /**
    * obj           对象
    */    
    public StopEffect(obj):void{
        egret.Tween.removeTweens(obj);
    }

    /**
     * 打开弹窗动画
     * 0：没有动画 1:从中间轻微弹出 2：从中间猛烈弹出  3：从左向右 4：从右向左 5、从上到下 6、从下到上
     */
    public OpenUIEffect(panel:any, effectType:number, isAlert:boolean = false)
    {
         //以下是弹窗动画
        let popUpWidth = panel.width;
        let popUpHeight = panel.height;
        let originalWidth = 1280;
        let originalHeight = 720;
        
        var leftX:number = originalWidth/2 - popUpWidth/2;
        var upY:number = originalHeight/2 - popUpHeight/2;

        switch(effectType){
            case 0:
                break;
            case 1:
                panel.alpha = 0;
                panel.scaleX = 0.5;
                panel.scaleY = 0.5;
                panel.x = panel.x + popUpWidth/4;
                panel.y = panel.y + popUpHeight/4;
                egret.Tween.get(panel).to({alpha:1,scaleX:1,scaleY:1,x:panel.x - popUpWidth/4,y:panel.y - popUpHeight/4},300,egret.Ease.backOut); 
                break;
            case 2:
                panel.alpha = 1;
                panel.scaleX = 0.5;
                panel.scaleY = 0.5;
                panel.x = panel.x + popUpWidth/4;
                panel.y = panel.y + popUpHeight/4;
                egret.Tween.get(panel).to({alpha:1,scaleX:1,scaleY:1,x:panel.x - popUpWidth/4,y:panel.y - popUpHeight/4},600,egret.Ease.elasticOut); 
                break;
            case 3:
                if(isAlert){
                    panel.x = - popUpWidth;
                    egret.Tween.get(panel).to({x:leftX},500,egret.Ease.cubicOut); 
                }else{       
                    panel.x = - popUpWidth;
                    egret.Tween.get(panel).to({x:0},500,egret.Ease.cubicOut);
                }
                break;
            case 4:
                if(isAlert){
                    panel.x = popUpWidth;
                    egret.Tween.get(panel).to({x:leftX},500,egret.Ease.cubicOut);  
                }else{
                    panel.x = popUpWidth;
                    egret.Tween.get(panel).to({x:0},500,egret.Ease.cubicOut);  
                }
                break;
            case 5:
                if(isAlert){
                    panel.y = - popUpHeight;
                    egret.Tween.get(panel).to({y:upY},500,egret.Ease.cubicOut); 
                }else{
                    panel.y = - popUpHeight;
                    egret.Tween.get(panel).to({y:0},500,egret.Ease.cubicOut);   
                }
                break;
            case 6:
                if(isAlert){
                    panel.y = originalHeight;
                    egret.Tween.get(panel).to({y:upY},500,egret.Ease.cubicOut); 
                }else{
                    panel.y = popUpHeight;
                    egret.Tween.get(panel).to({y:0},500,egret.Ease.cubicOut); 
                }
                break;
            case 7:
                panel.alpha = 0;
                panel.scaleX = 0;
                panel.anchorOffsetX = panel.width >> 1;
                panel.x = panel.width >> 1;
                panel.y = 0;
                egret.Tween.get(panel).to({scaleX:1, alpha:1},500,egret.Ease.backOut); 
            default:
                break;
        }		
    }

    /**
     * 关闭界面动画
     */
    public CloseUIEffect(panel:any, effectType:number, call:Function)
    {
         //以下是弹窗动画

        switch(effectType){
            case 0:
                break;
            case 1:
                egret.Tween.get(panel).to({alpha:0,scaleX:0,scaleY:0,x:panel.x + panel.width/2,y:panel.y + panel.height/2},300).call(call); 
                break;
            case 2:
		        egret.Tween.get(panel).to({x:panel.width},500,egret.Ease.cubicOut).call(call); 
                break;
            case 3:
		        egret.Tween.get(panel).to({x:-panel.width},500,egret.Ease.cubicOut).call(call);        
                break;
            case 4:
		        egret.Tween.get(panel).to({y:panel.height},500,egret.Ease.cubicOut).call(call);             
                break;
            case 5:
		        egret.Tween.get(panel).to({y:-panel.height},500,egret.Ease.cubicOut).call(call);              
                break;
            case 6:
                egret.Tween.get(panel).to({scaleX:0},200).call(call);; 
            default:
                break;
        } 		
    }

    /**
	 * 数字变化
	 * t Label或BitmapLabel
	 */
	public NumChange(t:any, begin:number, end:number, time:number = 500, tittle:string=''){
		if(begin == end){
			return;
		}
		var o = {n:begin};
		t.text = begin.toString();
		egret.Tween.get(o, {
			onChange:function (){
				t.text = tittle+Math.round(o.n).toString();
			}
		}, this).to({n:end}, time);
	}

     /**
    * 按钮特效
    * obj           对象
    */
    public playBtnEffect(obj):void{
        egret.Tween.get(obj).to({ scaleX: 0.6, scaleY: 0.6}, 100).call(()=>{
            egret.Tween.get(obj).to({ scaleX: 1, scaleY: 1}, 100).call(()=>{// egret.Ease.elasticOut
                if(obj.isPlayingAnim)
                    obj.isPlayingAnim = false;
            });
        });
    }

    //对象旋转特效
    //obj   旋转对象
    //time  旋转一周用时，毫秒
    public rotationEffect(obj,time:number = 1000):void{
        var onComplete1:Function = function(){
            if(obj != null){
                obj.rotation = 0;
                egret.Tween.get(obj).to({rotation:360},time).call(onComplete1,this);   
            }
        };
        obj.rotation = 0;
        egret.Tween.get(obj).to({rotation:360},time).call(onComplete1,this);   
    }

        /**
    * 显示对象上下浮动特效
    * obj           对象
    * time          浮动时间 毫秒
    * space         浮动高度
    * todo          多个对象跳动
    */    
    public flyObj(obj,time,space:number = 50):void{
        var onComplete1:Function = function(){
            if(obj != null){
                var onComplete2:Function = function(){
                    egret.Tween.get(obj).to({y:obj.y - space},time).call(onComplete1,this);
                };  
                egret.Tween.get(obj).to({y:obj.y + space},time).call(onComplete2,this);
            }
        };  
        onComplete1();
    }

    /**
    * 显示对象放大缩小忽明忽暗
    * obj           对象
    */    
    public ScaleEffect(obj):void{
        var onComplete1:Function = function(){
            if(obj != null){
                var onComplete2:Function = function(){
                    egret.Tween.get(obj).to({scaleX:1, scaleY:1, alpha:1},700).call(onComplete1,this);
                };  
                egret.Tween.get(obj).to({scaleX:1.3, scaleY:1.3, alpha:0.5},700).call(onComplete2,this);
            }
        };  
        onComplete1();
    }

        /**
    * 文字打字机效果
    * obj           文本对象
    * content       文字
    * interval      打字间隔 毫秒
    */    
    public typerEffect(obj,content:string = "",interval:number = 200):void{
        var strArr:Array<any> = content.split("");
        var len:number = strArr.length;
        for (var i = 0; i < len; i++){
            egret.setTimeout(function () {              
                obj.appendText(strArr[Number(this)]);
            }, i, interval*i);              
        }
    }

    //对象上下左右來回动
    public MoveEffect(obj):void{
        var onComplete1:Function = function(){
            if(obj != null){
                var onComplete2:Function = function(){
                    egret.Tween.get(obj).to({x:obj.x -30, y:obj.y - 30},500).call(onComplete1,this);   
                }
                egret.Tween.get(obj).to({x:obj.x +30, y:obj.y + 30},500).call(onComplete2,this);  
            }
        };
        onComplete1();
    }

    //对象左右來回动
    public MoveEffect2(obj):void{
        var onComplete1:Function = function(){
            if(obj != null){
                var onComplete2:Function = function(){
                    egret.Tween.get(obj).to({x:obj.x - 300},500).call(onComplete1,this);   
                }
                egret.Tween.get(obj).to({x:obj.x + 300},500).call(onComplete2,this);  
            }
        };
        onComplete1();
    }

    //抖动特效
    public shakeEffect(obj)
    {
        var onComplete1:Function = function(){
            if(obj != null){
                var onComplete2:Function = function(){
                    egret.Tween.get(obj).wait(1000).call(onComplete1,this);  
                }
                egret.Tween.get(obj).to({rotation:30},50).
                to({rotation:-30},100)
                .to({rotation:30},100)
                .to({rotation:-30},100)
                .to({rotation:30},100)
                .to({rotation:0},50)
                .call(onComplete2,this);  
            }
        };
        onComplete1();
    }

    //放大透明度变0再重复
    public scaleAlphaEffect(obj)
    {
        var onComplete1:Function = function(){
            if(obj != null){
                var onComplete2:Function = function(){
                    obj.scaleX = obj.scaleY = obj.alpha = 1;
                    egret.Tween.get(obj).wait(1000).call(onComplete1,this);  
                }
                egret.Tween.get(obj).to({
                    scaleX:1.8,
                    scaleY:1.8,
                    alpha:0
                },500)
                .call(onComplete2,this);  
            }
        };
        onComplete1();
    }

    //左右摇摆
    public rockEffect(obj)
    {
        var onComplete1:Function = function(){
            if(obj != null){
                obj.rotation = -20;
                var onComplete2:Function = function(){
                    egret.Tween.get(obj).to({
                    rotation:-20
                },2000).call(onComplete1,this);  
                }
                egret.Tween.get(obj).to({
                    rotation:20
                },2000)
                .call(onComplete2,this);  
            }
        };
        onComplete1();
    }

    //伪3d旋转
    public RotationTree(obj)
    {
        if(!obj) return;
        var onComplete1:Function = function(){
            if(obj != null){
                var onComplete2:Function = function(){
                    egret.Tween.get(obj).to({scaleX:1},250).call(onComplete1,this);   
                }
                egret.Tween.get(obj).to({scaleX:0},250).call(onComplete2,this);  
            }
        };
        onComplete1();
    }
}